﻿using System;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography;

namespace PMngr.Infrastructure
{
    public static class LinqExtensions
    {
        public static IQueryable<TSource> WhereIf<TSource>(
            this IQueryable<TSource> source,
            bool condition,
            Expression<Func<TSource, bool>> predicate)
        {
            if (condition)
                return source.Where(predicate);
            else
                return source;
        }
    }

    public static class StringExtensions
    {
        public static string MD5Encrypt(this string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
                sb.Append(data[i].ToString("x2"));

            return sb.ToString();
        }

        public static bool VerifyMD5(this string input, string hash)
        {
            string md5 = MD5Encrypt(input);
            return String.Compare(md5, hash, true) == 0;
        }
    }


}
